<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from '../../../../dashboard/mixins/resize'

const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b}: {c} ({d}%)',
          position:['50%', '50%'],
        },
        legend: {
          orient: 'vertical',
          right: 10,
          top: 30,
          bottom: 10,
          data: [
            '维修保养',
            '更换物料',
            '工艺调整',
            '无订单停机'
          ],
        },
        series: [
          {
            name: '设备停机原因',
            type: 'pie',
            radius: ['45%', '60%'],
            label: {
              show: false,
            },
            data: [
              { value: 1048, name: '维修保养' },
              { value: 251, name: '更换物料' },
              { value: 147, name: '工艺调整' },
              { value: 102, name: '无订单停机' }
            ],
            animationDuration,
            axisTick: {
              show: false
            },
            splitLine: {
              show: false
            },
            axisLabel: {
              show: false
            },
            itemStyle: { // 饼图白色间隙
              borderWidth: 1,
              borderColor: "#fff",
            },
          }
        ],
        grid: {
          left: '0%',
          right: '4%',
          top: '3%',
          bottom: '2%',
          containLabel: true
        },
      })
    }
  }
}
</script>
